package top.ztf.erp.utils;


import net.sf.cglib.beans.BeanCopier;

import java.util.HashMap;
import java.util.Map;

/**
 * 对象转换工具类
 */
public class BeanCopyUtil {

    /**
     * 缓存区
     */
    private static final Map<String, BeanCopier> buffer =  new HashMap<>();

    /**
     * 双重检测锁
     * @param source 拷贝的源
     * @param target 拷贝的目标
     */
    public static void copy(Object source,Object target){
        String name = source.getClass().toString()+target.getClass().toString();
        BeanCopier beanCopier = buffer.get(name);

        if (beanCopier==null){
            synchronized (BeanCopyUtil.class){
                beanCopier = buffer.get(name);
                if (beanCopier==null){
                    beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
                    buffer.put(name,beanCopier);
                }
            }
            beanCopier.copy(source,target,null);
        }else {
            beanCopier.copy(source,target,null);
        }
    }

}

